package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.mapper.AccountMapper;
import com.chenyaowei.happyhub.mapper.IndexNavMapper;
import com.chenyaowei.happyhub.pojo.IndexNav;
import com.chenyaowei.happyhub.pojo.wechatuser.Account;
import com.chenyaowei.happyhub.pojo.wechatuser.Order;
import com.chenyaowei.happyhub.service.AccountService;
import com.chenyaowei.happyhub.service.GoodsService.OrderService;
import com.chenyaowei.happyhub.utils.ArrowUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminUserController {
    @Resource
    IndexNavMapper indexNavMapper;
    @Resource
    AccountService accountService;
    @Resource
    OrderService orderService;
    PageInfo pageInfo;
    List<Account> accountList;
    String down = "ti-arrow-down";
    String vertical = "ti-arrows-vertical";
    String up = "ti-arrow-up";
    int pageSize=30;
    @RequestMapping("/account")
    String searchUser(Model model,String pageNum,String keyword,String isPay,String isLogin,String accountId){
        System.out.println(isLogin+isPay+accountId);
        if (pageNum==null|| Objects.equals(pageNum, ""))
            pageNum="1";
        if(isPay!=null||isLogin!=null)
            accountService.updateAccount(isLogin,isPay,accountId);
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        System.out.println(Integer.parseInt(pageNum)+" "+pageSize+" "+keyword);
        pageInfo=accountService.selectAccount(Integer.parseInt(pageNum),pageSize,keyword);
        accountList=pageInfo.getList();
        model.addAttribute("keyword", "");
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("accountList",accountList);
        return "account";
    }
    @RequestMapping("/order")
    String searchOrder(Model model,String pageNum,String keyword,String accountId,String sort,String status){
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        pageInfo=orderService.searchOrder(Integer.parseInt(pageNum),3,accountId,keyword,sort,status);
        List<Order> orderList=pageInfo.getList();
        ArrowUtils.setAttributeOrder(model,sort,status);
        model.addAttribute("orderList", orderList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("keyword", keyword);
        model.addAttribute("sort", sort);
        model.addAttribute("status", status);
        model.addAttribute("accountId", accountId);
        return "order";
    }
    @RequestMapping("/Order")
    String toOrder(Model model){
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        pageInfo=orderService.searchOrder(1,3,null,null,null,null);
        List<Order> orderList=pageInfo.getList();
        model.addAttribute("orderList", orderList);
        model.addAttribute("pageNum", "1");
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("indexNavList", indexNavList);
        model.addAttribute("keyword", "");
        model.addAttribute("sort", "");
        model.addAttribute("status", "");
        model.addAttribute("accountId", "");
        model.addAttribute("metaStatus", vertical);
        model.addAttribute("totalPriceStatus", vertical);
        model.addAttribute("goodNumStatus", vertical);
        model.addAttribute("dateStatus", vertical);
        return "order";
    }
    @RequestMapping("/toUpdateOrder")
    String toUpdateOrder(Model model,String orderId){
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        Order order=orderService.selectOrderByOrderId(Integer.parseInt(orderId));
        model.addAttribute("order",order);
        model.addAttribute("indexNavList", indexNavList);
        return "updateOrder";
    }
    @RequestMapping("/updateOrder")
    String updateOrder(Model model,String orderId,String receiverName,String mobile,String province,String city,String detail){
        List<IndexNav> indexNavList = indexNavMapper.selectAllType();
        System.out.println(orderId);
        System.out.println(receiverName);
        System.out.println(mobile);
        System.out.println(city);
        System.out.println(province);
        System.out.println(detail);
        orderService.updateOrder(Integer.parseInt(orderId),receiverName,mobile,province,city,detail);
        Order order=orderService.selectOrderByOrderId(Integer.parseInt(orderId));
        model.addAttribute("order",order);
        model.addAttribute("indexNavList", indexNavList);
        return "updateOrder";
    }
}
